<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*-----------------------------------------------------------------------------------------------
get_products_block()
-----------------------------------------------------------------------------------------------*/
class Get_products_block extends Front_Controller{

	function Get_products_block(){
        parent::Front_Controller();
		$CI = &get_instance();
		//$CI->load->module_language('blocks.block','english');
		
	}
	
	function index(){
        $CI = &get_instance();
		$direct_func =  $CI->uri->segment(2);
		if($direct_func=='Chi-Tiet'){
			$this->detail();
		}
		else{
			$fieldname = "";
			if($direct_func=='Linh-Vuc'){
				$field =  $CI->uri->segment(3);
				//SQL paging
				$q = $CI->db->get_where(COURSES, array(COURSES_FIELD=>$field));
				
				// pagenavigation
				$start = $CI->uri->segment(4)?$CI->uri->segment(4):0;
				$CI->load->library('pagination');
				$config['base_url'] = site_url('Khoa-Hoc/Linh-Vuc/'.$field.'/%s');
				$config['total_rows'] = $q->num_rows();
				$config['per_page'] = $CI->config->item('rows_per_page');
				$config['cur_page'] = $start;
				$config['uri_segment'] = $start;
				$config['num_links'] = $CI->config->item('num_links');		
				$CI->pagination->initialize($config);
				$this->add('paging', $CI->pagination->create_links());
				//end
				//SQL get product
				$CI->db->select('*');
				$CI->db->from(COURSES.' p');				    	
				$CI->db->where(COURSES_FIELD,$field);			
  	
				$CI->db->order_by('p.'.COURSES_NAME, 'ASC');
				$CI->db->limit($CI->config->item('rows_per_page'), $start);
				$q = $CI->db->get();						
				$products = $q->result();
				
				//SQL get group and categories
				$CI->db->select('p_g.*,p_c.'.COURSE_CATEGORIES_NAME.' as product_cate_name ');
				$CI->db->from(COURSE_FIELDS.' p_g');		
				$CI->db->join(COURSE_CATEGORIES.' p_c', 'p_g.'.COURSE_FIELD_CATEGORIES.' = p_c.'.COURSE_CATEGORIES_ID);		    	
				$CI->db->where(COURSE_FIELD_ID, $field);
				$q = $CI->db->get();
				$field_cate = $q->row();
				$fieldname = ($field_cate)?' : '.$field_cate->product_cate_name.' >> '.$field_cate->{COURSE_FIELD_NAME}:''; 
				$this->add('pg_name', $field_cate->{COURSE_FIELD_NAME});
			}			
			else{
				//$CI->load->module_model('blocks.products_model');				
				$CI->db->select('p.*');
				$CI->db->from(COURSES.' p');		
				$q = $CI->db->get();				
				// pagenavigation
				$start = $CI->uri->segment(2)?$CI->uri->segment(2):0;
				$CI->load->library('pagination');
				$config['base_url'] = site_url('Khoa-Hoc'.'/%s');
				$config['total_rows'] = $q->num_rows();
				$config['per_page'] = $CI->config->item('rows_per_page');
				$config['cur_page'] = $start;
				$config['num_links'] = $CI->config->item('num_links');		
				$CI->pagination->initialize($config);
				$this->add('paging', $CI->pagination->create_links());
				//end
				//SQL get product
				$CI->db->select('p.*,p_g.'.COURSE_FIELD_NAME.' as pg_name ');
				$CI->db->from(COURSES.' p');		
				$CI->db->join(COURSE_FIELDS.' p_g', 'p.'.COURSES_FIELD.' = p_g.'.COURSE_FIELD_ID);
				//$CI->db->where(' p.'.COURSES_PROMOTION_PRICE.' = 0 ', NULL, FALSE);
				$CI->db->order_by('p.'.COURSES_NAME, 'ASC');				
				$CI->db->limit($CI->config->item('rows_per_page'), $start);
				$q = $CI->db->get();			
				$products = $q->result();
				
			}			
			$product_image = base_url().'application/assets/products/1/editor/images/'; 
			$product_image_thumb = base_url().'application/assets/products/1/editor/images/thumb/'; 			
			$this->add('groupname', $fieldname);
			$this->add('products', $products);
			$this->add('product_image', $product_image);
			$this->add('product_image_thumb', $product_image_thumb);
			//Add language
			
			//echo $CI->config->item('language');
			//$this->add('_title_product_page', $CI->lang->line('not_found'));
			$this->add('_title_product_page', $CI->lang->line('_title_product_page'));
			
			$this->view(get_class($this));
		}
	}
	function detail(){
		$CI = &get_instance();
		$product_id = (int)$CI->uri->segment(3);
		$showimage = (int)$CI->uri->segment(4);
		if($product_id){
			$product_image = base_url().'application/assets/products/1/editor/images/'; 
			$product_image_thumb = base_url().'application/assets/products/1/editor/images/thumb/'; 
			$CI->load->module_model('blocks.products_model');
			$product = $CI->products_model->get_by_id($product_id);
			$this->add('product', $product);
			$this->add('product_image', $product_image);
			$this->add('product_image_thumb', $product_image_thumb);
		}
		else{
			$this->add('message',$CI->lang->line('not_found'));
			$this->view('get_blank_block');
			return false;
		}
		if($showimage!=1){
	        $this->view('get_product_detail');
		}
		else
			$this->view('get_product_detail');
	
	}
}